#include <gtest/gtest.h>
#include "../mqcommon/mq_msg.pb.h"
#include "../mqserver/mq_message.hpp"

mq_zxws::MessageManager::ptr mmp;

// class ExchangeTest: public testing::Environment
// {
// public:
//     virtual void SetUp()override{
//         mmp = std::make_shared<mq_zxws::MessageManager>("./data/message");
//         mmp->initQueueMessage("queue1");
//     }
//     virtual void TearDown()override{
//         mmp->clear();
//     }
// };

// TEST(msg_test, insert_test){
//     mq_zxws::BasicProperties properties;
//     properties.set_id(mq_zxws::UUID::uuid());
//     properties.set_deliver_mode(mq_zxws::DeliverMode::DURABLE);
//     properties.set_routing_key("news.music.pop");
//     mmp->insert("queue1", &properties, "hello world-1", mq_zxws::DeliverMode::DURABLE);
//     mmp->insert("queue1", nullptr, "hello world-2", mq_zxws::DeliverMode::UNDURABLE);
//     mmp->insert("queue1", nullptr, "hello world-3", mq_zxws::DeliverMode::DURABLE);
//     mmp->insert("queue1", nullptr, "hello world-4", mq_zxws::DeliverMode::DURABLE);
//     mmp->insert("queue1", nullptr, "hello world-5", mq_zxws::DeliverMode::DURABLE);
//     ASSERT_EQ(mmp->getable_count("queue1"), 5);
//     ASSERT_EQ(mmp->total_count("queue1"), 4);
//     ASSERT_EQ(mmp->durable_count("queue1"), 4);
//     ASSERT_EQ(mmp->waitack_count("queue1"), 0);
// }


mq_zxws::MessageManager::ptr mmp1;

class ExchangeTest1: public testing::Environment
{
public:
    virtual void SetUp()override{
        mmp1 = std::make_shared<mq_zxws::MessageManager>("./data/message");
        mmp1->initQueueMessage("queue1");
    }
    virtual void TearDown()override{
        // emp->clear();
    }
};
// TEST(msg_test1, select_test){
//     // ASSERT_EQ(mmp1->getable_count("queue1"), 4);
//     // ASSERT_EQ(mmp1->durable_count("queue1"), 4);
//     // mmp1->durable_count("queue1");
//     mq_zxws::MessagePtr msg1 = mmp1->front("queue1");
//     ASSERT_EQ(msg1->payload().body(), std::string("hello world-1"));
//     ASSERT_EQ(mmp1->waitack_count("queue1"), 1);
//     ASSERT_EQ(mmp1->getable_count("queue1"), 3);

//     // mq_zxws::MessagePtr msg2 = mmp->front("queue1");
//     // ASSERT_EQ(msg1->payload().body(), "hello world-2");
//     mq_zxws::MessagePtr msg3 = mmp1->front("queue1");
//     ASSERT_EQ(msg3->payload().body(), std::string("hello world-3"));
//     ASSERT_EQ(mmp1->waitack_count("queue1"), 2);
//     ASSERT_EQ(mmp1->getable_count("queue1"), 2);

//     mq_zxws::MessagePtr msg4 = mmp1->front("queue1");
//     ASSERT_EQ(msg4->payload().body(), std::string("hello world-4"));
//     ASSERT_EQ(mmp1->waitack_count("queue1"), 3);
//     ASSERT_EQ(mmp1->getable_count("queue1"), 1);
    
//     mq_zxws::MessagePtr msg5 = mmp1->front("queue1");
//     ASSERT_EQ(msg5->payload().body(), std::string(("hello world-5")));
//     ASSERT_EQ(mmp1->waitack_count("queue1"), 4);
//     ASSERT_EQ(mmp1->getable_count("queue1"), 0);

// }

TEST(msg_test1, delete_test){
    ASSERT_EQ(mmp1->getable_count("queue1"), 4);
    ASSERT_EQ(mmp1->durable_count("queue1"), 4);
    mq_zxws::MessagePtr msg1 = mmp1->front("queue1");
    ASSERT_EQ(mmp1->durable_count("queue1"), 4);
    ASSERT_EQ(mmp1->waitack_count("queue1"), 1);
    
    ASSERT_EQ(msg1->payload().body(), std::string("hello world-1"));
    mmp1->ack("queue1", msg1->payload().properties().id());
    ASSERT_EQ(mmp1->durable_count("queue1"), 3);
    ASSERT_EQ(mmp1->waitack_count("queue1"), 0);
}

int main(int argc, char*argv[])
{
    testing::InitGoogleTest(&argc, argv);
    // testing::AddGlobalTestEnvironment(new ExchangeTest);
    testing::AddGlobalTestEnvironment(new ExchangeTest1);
    int res = RUN_ALL_TESTS();
    return 0;
}